﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class SysSettingDA : ISysSettingDA
    {
        private string CREATED_BY = "@CREATED_BY";
        private string DESCR = "@DESCR";
        private string EFFECTIVE = "@EFFECTIVE";
        private string IS_MODIFIABLE = "@IS_MODIFIABLE";
        private string PARAM_TYPE = "@PARAM_TYPE";
        private string SQL_CHECK_SYS_SETTING_ID_UNIQUE = " SELECT COUNT(1) FROM @_@SYS_SETTING WHERE SYS_SETTING_ID = @SYS_SETTING_ID ";
        private string SQL_DELETE_SYS_SETTING = " DELETE FROM @_@SYS_SETTING WHERE SYS_SETTING_ID = @SYS_SETTING_ID ";
        private string SQL_INSERT_SYS_SETTING = " INSERT INTO @_@SYS_SETTING ( WH_ID, SYS_SETTING_ID, DESCR, VALUE, VALUE_DEFAULT, PARAM_TYPE, IS_MODIFIABLE, EFFECTIVE, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE) VALUES ( @WH_ID, @SYS_SETTING_ID, @DESCR, @VALUE, @VALUE_DEFAULT, @PARAM_TYPE, @IS_MODIFIABLE, @EFFECTIVE, @CREATED_BY, GETDATE(), @UPDATED_BY, GETDATE() )  ";
        private string SQL_SELECT_ALL_SYS_SETTING = " SELECT  SS.ROWID, SS.WH_ID, SS.SYS_SETTING_ID, SS.DESCR, T.[TEXT] AS DESCR_T, SS.VALUE, SS.VALUE_DEFAULT, SS.PARAM_TYPE, SS.IS_MODIFIABLE, SS.EFFECTIVE,  SS.TS, SS.CREATED_BY, SS.CREATED_DATE, SS.UPDATED_BY, SS.UPDATED_DATE   FROM  @_@SYS_SETTING SS LEFT JOIN sysadm.TRANSLATION T ON SS.SYS_SETTING_ID = T.ITEM_ID  AND  T.ITEM_TYPE = 'SYS_SETTING' AND T.LANG+'-'+T.COUNTRY = @LANGUAGE ";
        private string SQL_UPDATE_SYS_SETTING = " UPDATE @_@SYS_SETTING SET WH_ID = @WH_ID, DESCR = @DESCR, VALUE = @VALUE, VALUE_DEFAULT = @VALUE_DEFAULT, PARAM_TYPE = @PARAM_TYPE, IS_MODIFIABLE = @IS_MODIFIABLE, EFFECTIVE = @EFFECTIVE, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE() WHERE SYS_SETTING_ID = @SYS_SETTING_ID  ";
        private string SYS_SETTING_ID = "@SYS_SETTING_ID";
        private string TS = "@TS";
        private string UPDATED_BY = "@UPDATED_BY";
        private string VALUE = "@VALUE";
        private string VALUE_DEFAULT = "@VALUE_DEFAULT";
        private string WH_ID = "@WH_ID";

        public bool CheckSysSettingIDUnique(string sysSettingID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@SYS_SETTING_ID", sysSettingID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_SYS_SETTING_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckSysSettingIDUnique(DataBase dataBase, DbTransaction tran, string sysSettingID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@SYS_SETTING_ID", sysSettingID) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_SYS_SETTING_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public int DeleteSysSetting(List<string> sysSettingID, List<string> whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < sysSettingID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@SYS_SETTING_ID", sysSettingID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_SYS_SETTING.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeleteSysSetting(DataBase dataBase, DbTransaction tran, List<string> sysSettingID, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < sysSettingID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@SYS_SETTING_ID", sysSettingID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_SYS_SETTING.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public DataSet GetAllSysSetting(string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_SYS_SETTING.Replace("@_@", whLoginID);
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public DataSet GetAllSysSetting(DataBase dataBase, DbTransaction tran, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_SYS_SETTING.Replace("@_@", whLoginID);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql);
        }

        public SysSettingInfo GetSysSettingByID(string syssettingID, string whLoginID, PubParasInfo pubParasInfo)
        {
            string sql = this.SQL_SELECT_ALL_SYS_SETTING.Replace("@_@", whLoginID).Replace("@LANGUAGE", "'" + pubParasInfo.UserLanguage + "'") + " WHERE SYS_SETTING_ID = @SYS_SETTING_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@SYS_SETTING_ID", syssettingID) };
            SysSettingInfo sysSettingInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    sysSettingInfo = new SysSettingInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["SYS_SETTING_ID"].ToString(), reader["DESCR_T"].ToString(), reader["VALUE"].ToString(), reader["VALUE_DEFAULT"].ToString(), reader["PARAM_TYPE"].ToString(), reader["IS_MODIFIABLE"].ToString(), reader["EFFECTIVE"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
                }
            }
            return sysSettingInfo;
        }

        public SysSettingInfo GetSysSettingByID(DataBase dataBase, DbTransaction tran, string syssettingID, string whLoginID, PubParasInfo pubParasInfo)
        {
            string sql = this.SQL_SELECT_ALL_SYS_SETTING.Replace("@_@", whLoginID).Replace("@LANGUAGE", "'" + pubParasInfo.UserLanguage + "'") + " WHERE SYS_SETTING_ID = @SYS_SETTING_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@SYS_SETTING_ID", syssettingID) };
            SysSettingInfo sysSettingInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                sysSettingInfo = new SysSettingInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["SYS_SETTING_ID"].ToString(), reader["DESCR_T"].ToString(), reader["VALUE"].ToString(), reader["VALUE_DEFAULT"].ToString(), reader["PARAM_TYPE"].ToString(), reader["IS_MODIFIABLE"].ToString(), reader["EFFECTIVE"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return sysSettingInfo;
        }

        public DataSet GetSysSettingByQueryList(List<string> sqlWhere, SysSettingQueryEntity syssettingQuery, string whLoginID, PubParasInfo pubParasInfo)
        {
            string temp = this.SQL_SELECT_ALL_SYS_SETTING.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!syssettingQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, syssettingQuery.CurrentPage, syssettingQuery.PageSize, syssettingQuery.SortField, syssettingQuery.SortDirection).Replace("@LANGUAGE", "''" + pubParasInfo.UserLanguage + "''");
            }
            else
            {
                temp = temp.Replace("@LANGUAGE", "'" + pubParasInfo.UserLanguage + "'");
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetSysSettingByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, SysSettingQueryEntity syssettingQuery, string whLoginID, PubParasInfo pubParasInfo)
        {
            string temp = this.SQL_SELECT_ALL_SYS_SETTING.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!syssettingQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, syssettingQuery.CurrentPage, syssettingQuery.PageSize, syssettingQuery.SortField, syssettingQuery.SortDirection).Replace("@LANGUAGE", "''" + pubParasInfo.UserLanguage + "''");
            }
            else
            {
                temp = temp.Replace("@LANGUAGE", "'" + pubParasInfo.UserLanguage + "'");
            }
            return dataBase.ExecuteDataSet(tran, CommandType.Text, temp);
        }

        public int InsertSysSetting(SysSettingInfo sysSettingInfo, List<string> whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    sysSettingInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_SysSetting_Parameters(sysSettingInfo);
                    if (paras != null)
                    {
                        result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_SYS_SETTING.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int InsertSysSetting(DataBase dataBase, DbTransaction tran, SysSettingInfo sysSettingInfo, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    sysSettingInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_SysSetting_Parameters(sysSettingInfo);
                    if (paras != null)
                    {
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_SYS_SETTING.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_SysSetting_Parameters(SysSettingInfo sysSettingInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { new SqlParameter(this.WH_ID, SqlDbType.VarChar, 30), new SqlParameter(this.SYS_SETTING_ID, SqlDbType.VarChar, 30), new SqlParameter(this.DESCR, SqlDbType.VarChar, 200), new SqlParameter(this.VALUE, SqlDbType.VarChar, 30), new SqlParameter(this.VALUE_DEFAULT, SqlDbType.VarChar, 30), new SqlParameter(this.PARAM_TYPE, SqlDbType.VarChar, 1), new SqlParameter(this.IS_MODIFIABLE, SqlDbType.VarChar, 1), new SqlParameter(this.EFFECTIVE, SqlDbType.VarChar, 1), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 20), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 20) };
            if (!string.IsNullOrEmpty(sysSettingInfo.WhID))
            {
                paramArray[0].Value = sysSettingInfo.WhID;
            }
            else
            {
                paramArray[0].Value = DBNull.Value;
            }
            paramArray[1].Value = sysSettingInfo.SysSettingID;
            if (!string.IsNullOrEmpty(sysSettingInfo.Descr))
            {
                paramArray[2].Value = sysSettingInfo.Descr;
            }
            else
            {
                paramArray[2].Value = DBNull.Value;
            }
            paramArray[3].Value = sysSettingInfo.Value;
            paramArray[4].Value = sysSettingInfo.ValueDefault;
            paramArray[5].Value = sysSettingInfo.ParamType;
            paramArray[6].Value = sysSettingInfo.IsModifiable;
            paramArray[7].Value = sysSettingInfo.Effective;
            paramArray[8].Value = sysSettingInfo.CreatedBy;
            paramArray[9].Value = sysSettingInfo.UpdatedBy;
            return paramArray;
        }

        public int UpdateSysSetting(SysSettingInfo sysSettingInfo, List<string> whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    sysSettingInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_SysSetting_Parameters(sysSettingInfo);
                    if (paras != null)
                    {
                        result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_SYS_SETTING.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int UpdateSysSetting(DataBase dataBase, DbTransaction tran, SysSettingInfo sysSettingInfo, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    sysSettingInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_SysSetting_Parameters(sysSettingInfo);
                    if (paras != null)
                    {
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_SYS_SETTING.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }
    }
}

